/* Original Author:  Dominick O'Dierno
 * Date: 2007
 * Description: The Original Skinnable DOMercury Form
 * 
 * 
 * 
 * NOTE: After making changes to this, you will need to physically copy the 
 * SkinnableForm.dll file to the Interfaces Folder in the DOMercury bin/Debug 
 * in order to see the changes.
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DOMercury.Items;
using System.Xml;
using System.Xml.XPath;
using DOMercury.Managers;
using DOMercury.Interfaces;

namespace DOMercury.Forms
{
    public partial class Mercury : Form, IDOMercuryUserInterface
    {
        #region Private Members
        private bool ResetIndex = true;
        private SearchingForm fm = new SearchingForm();
        private Timer subIndexTimer;
        private SkinSet currentSkin;
        private ItemsList itemsList;
        private List<IAction> currentActions;
        private bool itemsActive = true;
        private Image currIcon;
        private Image pIcon;
        private Image ActionIcon;
        private bool textonly = false;
        private string type = "";
        string itemtext = "";
        private bool moving = false;
        private bool DR = ConfigManager.DescriptiveResults;
        private Point clickpoint;
        private Timer SearchTimer = new Timer();
        private bool persist = false;
        private System.Windows.Forms.Timer fadeTimer = null;
        private double fadeIncrement = .1;
        private ManagedWinapi.Windows.SystemWindow ForegroundWindow;

        #endregion
        public Mercury()
        {
            StartSkinnableForm();
        }

        private void StartSkinnableForm()
        {
            _singleton = this;
            ConfigManager.LoadCurrentConfiguration();
            SkinManager.ReadSkin(ConfigManager.CurrentSkin);
            currentSkin = SkinManager.CurrentSkin;
            InitializeComponent();
            ShowIcons(ConfigManager.ShowIcons);
            subIndexTimer = new Timer();
            subIndexTimer.Enabled = false;
            subIndexTimer.Interval = 250;
            subIndexTimer.Tick += new EventHandler(subIndexTimer_Tick);
            itemsList = new ItemsList(new List<PathItem>());
            fadeTimer = new Timer();
            fadeTimer.Enabled = false;
            fadeTimer.Tick += new EventHandler(fadeTimer_Tick);

            lbParams.BringToFront();
            
            SearchTimer.Interval = ConfigManager.STI;
            SearchTimer.Tick += new EventHandler(SearchTimer_Tick);

//            this.ContextMenuStrip = DOMercuryProgramFlowManager.Instance.ContextMenu;
            currentActions = new List<IAction>();
        }

        public void ActivateForm()
        {
            if (ConfigManager.Fade)
            {
                Opacity = 0;
            }
            ForegroundWindow = ManagedWinapi.Windows.SystemWindow.ForegroundWindow;
            GetSelectedItems();
            this.Show();
            if (ConfigManager.Fade)
            {
                fade(0, 1, 150);
            }
            this.Activate();
            cbitem.Focus();
        }

        public void DeactivateForm()
        {
            DeactivateDOMercury(true);
        }

        public void CloseForm()
        {
            this.Close();
        }

        public void DisplayOptions()
        {
            Forms.DisplayOptions dlg = new DisplayOptions();
            dlg.ShowDialog();
            DOMercury.Engine.Interface.EngineCore.RestartInterface();
        }

        public bool HasOptions
        {
            get
            {
                return true;
            }
        }

        //fade will have to be moved to within the form
        private void fade(double opacityFrom, double opacityTo, double time)
        {
            double steps = 20;
            Opacity = opacityFrom;

            double delay = time / steps;
            this.fadeIncrement = (opacityTo - opacityFrom) / steps;

            fadeTimer.Interval = (int)delay;
            fadeTimer.Start();

        }
        void fadeTimer_Tick(object sender, EventArgs e)
        {
            Opacity += this.fadeIncrement;

            if ((this.fadeIncrement > 0 && Opacity >= 1) || (this.fadeIncrement < 0 && Opacity <= 0))
            {
                fadeTimer.Stop();
            }
        }

        void subIndexTimer_Tick(object sender, EventArgs e)
        {
            subIndexTimer.Stop();
            subIndexTimer.Enabled = false;
            if (persist)
            {
                fm.Show();
            }
        }
        #region Single Form Instance Code

        private static volatile Mercury _singleton;
        private static object syncRoot = new Object();

        public static Mercury Current
        {
            get
            {
                return _singleton;
            }
            set
            {
                _singleton = value;
            }
        }   

        #endregion
        #region Forms Management Functions
        public void ResetForm()
        {
            textonly = false;
            if (!(lbItems.DataSource == null))
            {
                ((List<IItem>)(lbItems.DataSource)).Clear();
            }
            pnlShadow.BackColor = Color.DarkGray;
            gbObjects_Enter(new object(), new EventArgs());
            itemsActive = true;
            if (cbitem.AutoCompleteSource != DOMercury.Engine.Interface.EngineCore.ItemAutoCompleteSource(type))
            {
                cbitem.AutoCompleteSource = DOMercury.Engine.Interface.EngineCore.ItemAutoCompleteSource(type);
                cbitem.SelectionStart = cbitem.Text.Length;
                cbitem.SelectionLength = 0;
            }
            TurnOffcbItem();     
            lbItems.Visible = false;
            type = "";
            cbitem.Text = "";
            itemtext = "";
            cbParameters.Text = "";
            lblParameters.Text = "";
            lblDescription.Text = "";
            lblCurrentType.Text = "";
            lblCurrentItem.Text = "";
            lblItemDesc.Text = "";
            lblPItem.Text = "";
            lblPType.Text = "";
            lblPDesc.Text = "";
            pnlpIcon.Visible = false;
            currIcon = null;
            pIcon = null;
            lbItems.Focus();         
        }

        private void TurnOffcbItem()
        {
            cbitem.Visible = false;
            pnlIcon.Visible = false;
            pnlParentItem.Visible = false;
            lbObjects.Visible = true;
            lbObjects.Focus();
        }

        private void TurnOncbItem()
        {
            cbitem.Visible = true;

            if (lbItems.Visible)
            {
                pnlIcon.Visible = true;
            }
            else
            {
                pnlIcon.Visible = false;
            }
            pnlParentItem.Visible = true;
            lbObjects.Visible = false;
            cbitem.Focus();
        }

        private void ChangeIcon()
        {
            try
            {
                currIcon = DOMercury.Engine.Interface.EngineCore.GetIconImage(((IItem)(lbItems.SelectedItem)).DisplayDesc);
            }
            catch { }
            if (currIcon != null)
            {
                pnlIcon.Visible = true;
                pnlIcon.Invalidate();
                if ((currentSkin.ItemIconVisible) && (pIcon == null))
                {
                    pnlItemIcon.Visible = true;
                    pnlItemIcon.Invalidate();
                    
                }
            }
            else
            {
                pnlIcon.Visible = false;
                pnlItemIcon.Visible = false;
            }
        }

        private void ChangePIcon()
        {
            try
            {
                pIcon = DOMercury.Engine.Interface.EngineCore.GetIconImage(DOMercury.Engine.Interface.EngineCore.ParentItem.DisplayDesc);
            }
            catch { }
            if (pIcon != null)
            {
                pnlpIcon.Visible = true;
                pnlpIcon.Invalidate();
                if (currentSkin.ItemIconVisible)
                {
                    pnlItemIcon.Visible = true;
                    pnlItemIcon.Invalidate();
                }
            }
            else
            {
                pnlpIcon.Visible = false;
                pnlpIcon.Invalidate();
            }
        }

        private void ChangeDescriptions(bool sender)
        {
            IAction a = ((IAction)(cbAction.SelectedItem));
            if (a != null)
            {
                if (a.Description == "Executes the Alias")
                {
                    IItem i = ((IItem)(lbItems.SelectedItem));
                    if (i.ItemType == "alias")
                    {
                        lblDescription.Text = ((Alias)(lbItems.SelectedItem)).Description;
                        lblParameters.Text = ((Alias)(lbItems.SelectedItem)).Parameters;
                    }

                }
                else
                {
                    if (currentSkin.ActionIconVisible && !sender)
                    {
                        try
                        {
                            ActionIcon = DOMercury.Engine.Interface.EngineCore.GetIconImage(((IAction)(cbAction.SelectedItem)).ActionType);
                            pnlActionIcon.Visible = true;
                            pnlActionIcon.Invalidate();
                        }
                        catch
                        {
                            pnlActionIcon.Visible = false;
                        }
                    }
                    lblDescription.Text = a.Description;
                    lblParameters.Text = a.ParamDescription;

                }
            }
            else
            {
                lblDescription.Text = "";
                lblParameters.Text = "";
            }
            
        }

        private void ClearDescriptions()
        {
            lblDescription.Text = "";
            lblParameters.Text = "";
        }

        public void ShowIcons(bool Show)
        {
            if (Show)
            {
                lbItems.DrawMode = DrawMode.OwnerDrawFixed;
                lbObjects.DrawMode = DrawMode.OwnerDrawFixed;
            }
            else
            {
                lbItems.DrawMode = DrawMode.Normal;
                lbObjects.DrawMode = DrawMode.Normal;
            }
        }

        #endregion
        #region Functionality Management Functions
        public int SearchTimerInterval
        {
            get
            {
                return SearchTimer.Interval;
            }
            set
            {
                SearchTimer.Interval = value;
            }
        }
 
        public bool ChangeIndex()
        {
            try
            {
                persist = true;
                fm.Show();
                bool result = DOMercury.Engine.Interface.EngineCore.SetSubIndex(((IItem)(lbItems.SelectedItem)), itemtext, ((IAction)(cbAction.SelectedItem)), DOMercury.Engine.Interface.EngineCore.ProvideTextItem("")[0]);
                if (result)
                {
                    cbitem.Text = "";
                    UpdateParentItem();
                    itemtext = "";
                    UpdatelbItemsAll();
                }
                if (fm.Visible)
                {
                    fm.Hide();
                }
                persist = false;
                return result;
            }
            catch
            {
                return false;
            }
            
        }

        public void PreviousIndex()
        {
            textonly = false;
            cbitem.Text = DOMercury.Engine.Interface.EngineCore.ParentText;
            itemtext = DOMercury.Engine.Interface.EngineCore.ParentText;
            //if (cbitem.Text.Length > 0)
            //{
                cbitem.SelectionStart = cbitem.Text.Length;
            //}
            try
            {

                DOMercury.Engine.Interface.EngineCore.PreviousIndex();
               
            }
            catch
            {
                //return false;
            }
            UpdateParentItem();
            if (cbitem.Text != "")
            {
                UpdatelbItems();
            }
            else
            {
                UpdatelbItemsAll();
            }
            pnlParentItem.Visible = false;
            pnlParentItem.Visible = true;
        }

        public void UpdateParentItem()
        {
            try
            {
                IItem it = DOMercury.Engine.Interface.EngineCore.ParentItem;
                pIcon = DOMercury.Engine.Interface.EngineCore.GetIconImage(it.DisplayDesc);
                lblPDesc.Text = it.DisplayDesc;
                lblPItem.Text = it.DisplayName;
                lblPType.Text = it.ItemType;
                ChangePIcon();
            }
            catch
            {
                pnlParentItem.Visible = false;
                pIcon = null;
                lblPDesc.Text = "";
                lblPItem.Text = "";
                lblPType.Text = "";
            }
        }
        
        //Move to Managers
        public void GetSelectedItems()
        {
            lbObjects.Items.Clear();
            itemsList = DOMercury.Engine.Interface.EngineCore.GetSelectedItems();
            lbObjects.DisplayMember = "DisplayName";
            foreach (PathItem p in itemsList.Items)
            {
                lbObjects.Items.Add(p);
            }
            ObjectsListSize();
            
        }

        public void GetSelectedItems(ItemsList clipFileDropList)
        {
            lbObjects.Items.Clear();
            itemsList = clipFileDropList;
            lbObjects.DisplayMember = "DisplayName";
            foreach (PathItem p in itemsList.Items)
            {
                lbObjects.Items.Add(p);
            }
            ObjectsListSize();

        }

        private void UpdatelbItems()
        {
            lbItems.SuspendLayout();
            lbItems.DataSource = DOMercury.Engine.Interface.EngineCore.GetItems(itemtext);
            lbItems.DisplayMember = "DisplayName";
            
            ItemsListSize();
            lbItems.ResumeLayout();
            if (((List<IItem>)(lbItems.DataSource)).Count < 1)
            {
                lbItems.Visible = false;
            }
            else
            {
                lbItems.Visible = true;
            }
        }

        private void UpdatelbItemsAll()
        {
            UpdatelbItems();
        }

        private void ItemsListSize()
        {
            
            int nh = lbItems.ItemHeight * ((List<IItem>)(lbItems.DataSource)).Count;
            if (nh < currentSkin.ItemsBoxHeight)
            {
                lbItems.Size = new Size(lbItems.Size.Width, nh + lbItems.ItemHeight);
            }
            else
            {
                lbItems.Size = new Size(lbItems.Size.Width, currentSkin.ItemsBoxHeight);
            }
        }

        private void ObjectsListSize()
        {

            int nh = lbObjects.ItemHeight * lbObjects.Items.Count;
            if (nh < currentSkin.SelectedItemsBoxHeight)
            {
                lbObjects.Size = new Size(lbObjects.Size.Width, nh);
            }
            else
            {
                lbObjects.Size = new Size(lbObjects.Size.Width, currentSkin.SelectedItemsBoxHeight);
            }
        }

        private void discoverType()
        {
            itemsActive = false;
            //determine active object(s)
            if ((cbitem.Text == "") && (lbObjects.Items.Count > 0) && (DOMercury.Engine.Interface.EngineCore.ParentItem == null))
            {
                itemsActive = true;
            }
            //determine object(s) type
            if (itemsActive)
            {
                type = itemsList.ItemType;
                pnlCurrentItem.Visible = false;
                pnlParentItem.Visible = false;
            }
            else
            {
                pnlCurrentItem.Visible = true;
                if (DOMercury.Engine.Interface.EngineCore.ParentItem != null)
                {
                    pnlParentItem.Visible = true;
                }
                else
                {
                    pnlParentItem.Visible = false;
                }
                if (lbItems.Items.Count > 0)
                {
                    type = ((IItem)(lbItems.SelectedItem)).ItemType;
                }
                else
                {
                    type = "text";
                }
            }
            if (lbItems.Items.Count > 0)
            {
                lblCurrentItem.Text = ((IItem)(lbItems.SelectedItem)).DisplayName;
                lblCurrentType.Text = type;
                lblItemDesc.Text = ((IItem)(lbItems.SelectedItem)).DisplayDesc;
            }
            else
            {
                lblCurrentItem.Text = "";
                lblCurrentType.Text = type;
                lblItemDesc.Text = "";
            }
        }

        private void getActions()
        {
            if (!DOMercury.Engine.Interface.EngineCore.GetActions(type, ref currentActions))
            {
                currentActions = new List<IAction>();
            }
            cbAction.DataSource = currentActions;
            cbAction.DisplayMember = "ActionType";

            ClearDescriptions();
        }

        private void UpdateParams()
        {
            List<IItem> parameters;
            AutoCompleteMode acm;
            AutoCompleteSource acs;
            AutoCompleteStringCollection customsource;
            DOMercury.Engine.Interface.EngineCore.ChangeParametersAutoCompleteSource(out parameters, out acm, out acs, out customsource, ((IAction)(cbAction.SelectedItem)).ParameterType, type);
            cbParameters.AutoCompleteCustomSource = customsource;
            cbParameters.AutoCompleteMode = acm;
            cbParameters.AutoCompleteSource = acs;
            lbParams.DataSource = parameters;
            lbParams.DisplayMember = "DisplayName";
            
        }

        private void EnterButtonHandler()
        {
            //try to execute action with object(s)
            
            
            if (itemsActive)
            {
                if (!lbParams.Visible)
                {
                    ResetIndex = DOMercury.Engine.Interface.EngineCore.Perform(itemsList, ((IAction)cbAction.SelectedItem), DOMercury.Engine.Interface.EngineCore.FindPath(cbParameters.Text));
                }
                else
                {
                    ResetIndex = DOMercury.Engine.Interface.EngineCore.Perform(itemsList, ((IAction)cbAction.SelectedItem), lbParams.SelectedItem);
                }
            }
            else if (lbParams.DataSource != null)
            {
                ResetIndex = DOMercury.Engine.Interface.EngineCore.Perform(((IItem)lbItems.SelectedItem), ((IAction)cbAction.SelectedItem), lbParams.SelectedItem);
            }

            else if (lbItems.DataSource == null)
            {
                ResetIndex = DOMercury.Engine.Interface.EngineCore.Perform(((IItem)(DOMercury.Engine.Interface.EngineCore.ProvideTextItem(itemtext))), ((IAction)cbAction.SelectedItem), DOMercury.Engine.Interface.EngineCore.FindPath(cbParameters.Text));
            }
            else
            {
                ResetIndex = DOMercury.Engine.Interface.EngineCore.Perform(((IItem)lbItems.SelectedItem), ((IAction)cbAction.SelectedItem), DOMercury.Engine.Interface.EngineCore.FindPath(cbParameters.Text));

            }
            
            DeactivateDOMercury(false);
        }

        private void ActivateDOMercury()
        {
            lbObjects.SelectionMode = SelectionMode.None;
            if (lbObjects.Items.Count < 1)
            {
                TurnOncbItem();
            }
            if (!(itemsList.Items.Count > 0))
            {
                lblCurrentItem.Text = "";
                lblItemDesc.Text = "";
            }
            if (lbObjects.Items.Count > 0)
            {
                discoverType();
                getActions();
            }
            if (itemsActive)
            {
                lbObjects.Focus();
            }
            else
            {
                cbitem.Focus();
            }
        }

        private void DeactivateDOMercury(bool reset)
        {
            if (reset)
            {
                Engine.Interface.EngineCore.ReturnMainIndex();
            }
            this.Hide();
            ResetForm();
        }

        private void FormKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {
                cbAction_Enter(sender, new EventArgs());
            }
            else if (e.KeyCode == Keys.Back)
            {
                if (cbitem.Text.Length > 0)
                {
                    TurnOncbItem();
                    cbitem.SelectionStart = cbitem.Text.Length;
                    cbitem.SelectionLength = 0;
                    SendKeys.Send("{BACKSPACE}");
                }
            }
            else if ((e.KeyCode != Keys.Escape) || (e.KeyCode != Keys.ShiftKey))
            {
                TurnOncbItem();
                string ser = SendKeysHelper.KeyCodetoString(e.KeyCode);
                if (ser != "")
                {
                    SendKeys.Send(ser);
                }
                if (e.KeyValue == 190)
                {
                    SendKeys.Send(".");
                }
            }
            else if ((e.KeyCode == Keys.T))
            {
                if ((e.Control))
                {
                    textonly = !textonly;
                    e.SuppressKeyPress = true;
                    ProvideTextItem();
                }
            }
            else if ((e.KeyCode == Keys.V))
            {
                if ((e.Control))
                {
                    SetInputFromClipboard();
                }
            }
        }
        private void FormKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                DeactivateDOMercury(true);

            }
            else if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {
                //Handle Execute function
                EnterButtonHandler();
            }
        }

        private void SetInputFromClipboard()
        {
            if (Clipboard.ContainsText())
            {
               cbitem.Text = Clipboard.GetText();
            }
            else if (Clipboard.ContainsFileDropList())
            {
                List<DOMercury.Items.PathItem> pathItems = new List<PathItem>();
                System.Collections.Specialized.StringCollection paths = Clipboard.GetFileDropList();
                foreach (string path in paths)
                {
                    pathItems.Add(new PathItem(path, System.IO.Path.GetFileName(path)));
                }
                DOMercury.Items.ItemsList pathsItem = new ItemsList(pathItems);
                GetSelectedItems(pathsItem);
            }
        }


        private void ObjectsEnter()
        {
            //Gotta handle fonts somewhere else.
            lblItems.ForeColor = currentSkin.ActivePanelColor;
            pnlParentItem.ForeColor = currentSkin.ActivePanelColor;
            pnlCurrentItem.ForeColor = currentSkin.ActivePanelColor;
            lblActions.ForeColor = currentSkin.InactivePanelColor;
            lblDescription.ForeColor = currentSkin.InactivePanelColor;
            lblParams.ForeColor = currentSkin.InactivePanelColor;
            lblParameters.ForeColor = currentSkin.InactivePanelColor;
            lbParams.Visible = false;
        }
        private void ActionsEnter()
        {
            lblActions.ForeColor = currentSkin.ActivePanelColor;
            lblDescription.ForeColor = currentSkin.ActivePanelColor;
            pnlParentItem.ForeColor = currentSkin.InactivePanelColor;
            pnlCurrentItem.ForeColor = currentSkin.InactivePanelColor;
            lblItems.ForeColor = currentSkin.InactivePanelColor;
            lblParams.ForeColor = currentSkin.InactivePanelColor;
            lblParameters.ForeColor = currentSkin.InactivePanelColor;
            lbParams.Visible = false;

        }
        private void ParamsEnter()
        {
            lblParams.ForeColor = currentSkin.ActivePanelColor;
            pnlParentItem.ForeColor = currentSkin.InactivePanelColor;
            pnlCurrentItem.ForeColor = currentSkin.InactivePanelColor;
            lblActions.ForeColor = currentSkin.InactivePanelColor;
            lblDescription.ForeColor = currentSkin.InactivePanelColor;
            lblItems.ForeColor = currentSkin.InactivePanelColor;
            lblParameters.ForeColor = currentSkin.ActivePanelColor;

        }
        #endregion

#region Event Handlers
        #region Mercury Form
        public bool hider = false;
        private void Mercury_Load(object sender, EventArgs e)
        {
            
        }

        private void Mercury_Deactivate(object sender, EventArgs e)
        {
//#if DEBUG

//#else
            if (!persist)
            {
                DeactivateDOMercury(true);
            }
//#endif
        }

        private void Mercury_Activated(object sender, EventArgs e)
        {
            ActivateDOMercury();

        }
        private void Mercury_Shown(object sender, EventArgs e)
        {
            
            
        }

        private void Mercury_KeyDown(object sender, KeyEventArgs e)
        {
            FormKeyDown(sender, e);
        }

        private void Mercury_KeyUp(object sender, KeyEventArgs e)
        {
            FormKeyUp(sender, e);
        }

#endregion
        #region GroupBox Color
        private void gbObjects_Enter(object sender, EventArgs e)
        {
            ObjectsEnter();
        }

        private void gbAction_Enter(object sender, EventArgs e)
        {
            ActionsEnter();
        }

        private void gbParameters_Enter(object sender, EventArgs e)
        {
            ParamsEnter();
        }

        #endregion
        #region lbObjects
        private void lbObjects_DrawItem(object sender, DrawItemEventArgs e)
        {
            //e.Graphics.FillRectangle(Brushes.White, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
            try
            {
                e.Graphics.FillRectangle(currentSkin.SelectedItemsBoxBackColor, e.Bounds);
                e.Graphics.DrawString(itemsList.Items[e.Index].DisplayName, currentSkin.SelectedItemsBoxFont,currentSkin.SelectedItemsBoxFontColor, 16, e.Bounds.Top);
            }
            catch
            {

            }
            try
            {
                Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage(itemsList.Items[e.Index].DisplayDesc);
                if (!(tmp == null))
                {
                    e.Graphics.DrawImage(tmp, e.Bounds.Left, e.Bounds.Top, lbObjects.ItemHeight, lbObjects.ItemHeight);
                }
            }
            catch
            {
                //No icon to draw
            }
        }

        #endregion
        #region cbItem
        private void cbitem_KeyDown(object sender, KeyEventArgs e)
        {
            
            if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {

            }
            else if (e.KeyCode == Keys.Down)
            {
                

                if ((lbItems.Items.Count > 0) && (!e.Shift))
                {
                    if (lbItems.SelectedIndex == -1)
                    {
                        lbItems.SelectedIndex = 0;
                    }
                    else if (lbItems.SelectedIndex < lbItems.Items.Count - 1)
                    {
                        lbItems.SelectedIndex++;
                    }
                    if (!e.Shift)
                    {
                        e.SuppressKeyPress = true;
                    }
                }
            }
            else if (e.KeyCode == Keys.Up)
            {
                if ((lbItems.Items.Count > 0) && (!e.Shift))
                {
                    if (lbItems.SelectedIndex == -1)
                    {
                        lbItems.SelectedIndex = 0;
                    }
                    else if (lbItems.SelectedIndex > 0)
                    {
                        lbItems.SelectedIndex--;
                    }
                    if (!e.Shift)
                    {
                        e.SuppressKeyPress = true;
                    }
                }
            }
            if ((type == "folder") && !(itemtext.Contains(":")))
            {
                if (e.KeyCode == Keys.Down)
                {
                    if (lbItems.Items.Count > 0)
                    {
                        if (lbItems.SelectedIndex == -1)
                        {
                            lbItems.SelectedIndex = 0;
                        }
                        else if (lbItems.SelectedIndex < lbItems.Items.Count - 1)
                        {
                            lbItems.SelectedIndex++;
                        }

                    }
                }
                
            }
            else if ((e.KeyCode == Keys.T))
            {
                if ((e.Control))
                {
                    //Console.WriteLine("textonly: " + textonly.ToString());
                    textonly = !textonly;
                    e.SuppressKeyPress = true;
                    ProvideTextItem();
                }
            }
        }
        private void cbitem_KeyUp(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Escape)
            {
                DeactivateDOMercury(true);
                
            }
            else if ((e.KeyCode == Keys.Right))
            {
                ChangeIndex();
                e.SuppressKeyPress = true;
            }
            else if (/*(e.Alt) && */(e.KeyCode == Keys.Left))
            {
                PreviousIndex();
                e.SuppressKeyPress = true;
            }
            
            else if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {
                //SendKeys.Send("{TAB}");
                if (cbAction.DroppedDown)
                {
                    cbAction.DroppedDown = false;
                }
                
                //Handle Execute function
                EnterButtonHandler();
            }
            else if ((e.KeyCode == Keys.Delete))
            {
                cbitem.Text = "";
                itemtext = "";
            }
           
            
        } 

        private void cbitem_Enter(object sender, EventArgs e)
        {
            gbObjects_Enter(sender, e);
            cbitem.Visible = true;
        }

        private void cbitem_Leave(object sender, EventArgs e)
        {
            
        }

        private void cbitem_TextUpdate(object sender, EventArgs e)
        {
            if (!textonly)
            {
                if (cbitem.Text.Length == 1)
                {
                    SearchTimer_Tick(sender, e);
                }
                else
                {

                    SearchTimer.Stop();
                    SearchTimer.Start();
                }
            }
            else
            {
                itemtext = cbitem.Text;
                ProvideTextItem();
            }
        }

        private void ProvideTextItem()
        {

            List<IItem> txtitem = DOMercury.Engine.Interface.EngineCore.ProvideTextItem(itemtext);
            //txtitem.Add(new TextItem(itemtext));
            lbItems.SuspendLayout();
            lbItems.DataSource = txtitem;
            lbItems.DisplayMember = "DisplayName";
            ItemsListSize();
            lbItems.ResumeLayout();
        }

        private void cbitem_SelectedIndexChanged(object sender, EventArgs e)
        {
            //cbitem_TextUpdate(sender, e);
            itemtext = cbitem.SelectedText;
            ChangeDescriptions(true);
        }

        #endregion
        #region lbItems
        private void lbItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlCurrentItem.Visible = true;
            ChangeIcon();
            discoverType();
            getActions();
            ChangeDescriptions(true);
            //pnlCurrentItem.Visible = false;
            //pnlCurrentItem.Visible = true;
        }

        private void lbItems_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                ResetForm();
                this.Hide();
                
            }
            else if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {
                //SendKeys.Send("{TAB}");
                if (cbAction.DroppedDown)
                {
                    cbAction.DroppedDown = false;
                }

                //Handle Execute function
                EnterButtonHandler();
            }
            else
            {
                cbitem.Focus();
                cbitem.SelectionStart = cbitem.Text.Length;
                cbitem.SelectionLength = 0;
            }
        }

        public void DescriptiveResults(bool dr)
        {
            DR = dr;
            if (dr)
            {
                this.lbItems.ItemHeight = System.Convert.ToInt32(lbItems.Font.SizeInPoints * 4); //old * 2
            }
            else
            {
                this.lbItems.ItemHeight = System.Convert.ToInt32(lbItems.Font.SizeInPoints * 2); //old * 2
            }
            this.lbParams.ItemHeight = lbItems.ItemHeight;

        }

        private void lbItems_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                IItem temp = ((List<IItem>)(lbItems.DataSource))[e.Index];
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {                        
                    e.Graphics.FillRectangle(currentSkin.ItemsBoxSelectedBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                    if (DOMercury.Engine.Interface.EngineCore.HasSubIndex(temp.ItemType))
                    {
                        Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage("Dive Arrow");
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxSelectedTextBrush, lbItems.ItemHeight + 10, e.Bounds.Top);
                        e.Graphics.DrawImage(tmp, lbItems.ItemHeight, e.Bounds.Top, 10, 10);
                    }
                    else
                    {
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxSelectedTextBrush, lbItems.ItemHeight, e.Bounds.Top);
                    }
                    if (DR)
                    {
                        Font f = new Font(currentSkin.ItemsBoxFont, FontStyle.Italic);
                        e.Graphics.DrawString(temp.DisplayDesc, f, currentSkin.ItemsBoxSelectedTextBrush, lbItems.ItemHeight * 1.5F, e.Bounds.Top + (lbItems.ItemHeight / 2));
                    }
                    try
                    {
                        Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage(temp.DisplayDesc);
                        if (!(tmp == null))
                        {
                            e.Graphics.DrawImage(tmp, e.Bounds.Left, e.Bounds.Top, lbItems.ItemHeight, lbItems.ItemHeight);
                        }
                    }
                    catch
                    {
                        //No icon to draw
                    }
                }
                else
                {
                    e.Graphics.FillRectangle(currentSkin.ItemsBoxBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                    if (DOMercury.Engine.Interface.EngineCore.HasSubIndex(temp.ItemType))
                    {
                        Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage("Dive Arrow");
                        e.Graphics.FillRectangle(currentSkin.ItemsBoxBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxTextBrush, lbItems.ItemHeight + 10, e.Bounds.Top);
                        e.Graphics.DrawImage(tmp, lbItems.ItemHeight, e.Bounds.Top, 10, 10);
                    }
                    else
                    {
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxTextBrush, lbItems.ItemHeight, e.Bounds.Top);
                    }
                    if (DR)
                    {
                        Font f = new Font(currentSkin.ItemsBoxFont, FontStyle.Italic);
                        e.Graphics.DrawString(temp.DisplayDesc, f, currentSkin.ItemsBoxTextBrush, lbItems.ItemHeight * 1.5F, e.Bounds.Top + (lbItems.ItemHeight / 2));
                    }
                    try
                    {
                        Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage(temp.DisplayDesc);
                        if (!(tmp == null))
                        {
                            if ((e.Index == lbItems.SelectedIndex + 1) || (e.Index == lbItems.SelectedIndex - 1))
                            {
                                e.Graphics.DrawImage(tmp, e.Bounds.Left + 5, e.Bounds.Top + 5, lbItems.ItemHeight - 10, lbItems.ItemHeight - 10);
                            }
                            else
                            {
                                e.Graphics.DrawImage(tmp, e.Bounds.Left + 8, e.Bounds.Top + 8, lbItems.ItemHeight - 16, lbItems.ItemHeight - 16);
                            }
                        }
                    }
                    catch
                    {
                        //No icon to draw
                    }
                }

               
            }
            catch
            {
                //something terrible happened, do not draw
            }
        }

        private void lbItems_DoubleClick(object sender, EventArgs e)
        {
            EnterButtonHandler();
        }

        #endregion
        #region cbAction
        private void cbAction_Enter(object sender, EventArgs e)
        {
            gbAction_Enter(sender, e);
            cbAction.DroppedDown = true;
        }

        private void cbAction_Leave(object sender, EventArgs e)
        {
            
        }
        private void cbAction_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            ChangeDescriptions(false);
            UpdateParams();
            
            
        }

        private void cbAction_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                ResetForm();
                this.Hide();
                
            }
            else if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {
                //SendKeys.Send("{TAB}");
                if (cbAction.DroppedDown)
                {
                    cbAction.DroppedDown = false;
                }

                //Handle Execute function
                EnterButtonHandler();
            }
            else
            {
                switch (e.KeyCode)
                {
                    case Keys.Right:
                        cbParameters.Focus(); break;
                    case Keys.Left:
                        if (cbitem.Visible)
                        {
                            cbitem.Focus();
                        }
                        else
                        {
                            lbObjects.Focus();
                        }
                        break;

                    case Keys.Up:
                    case Keys.Down:
                        break;
                    default: break;

                }
            }
        }

        void cbAction_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            try
            {
                IAction temp = ((List<IAction>)(cbAction.DataSource))[e.Index];
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    e.Graphics.FillRectangle(currentSkin.ItemsBoxSelectedBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                    e.Graphics.DrawString(temp.ActionType, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxSelectedTextBrush, cbAction.ItemHeight + 2f, e.Bounds.Top);

                }
                else
                {
                    e.Graphics.FillRectangle(currentSkin.ItemsBoxBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                    e.Graphics.DrawString(temp.ActionType, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxTextBrush,cbAction.ItemHeight + 2f, e.Bounds.Top);

                }

                try
                {
                    Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage(temp.ActionType);
                    if (!(tmp == null))
                    {
                        e.Graphics.DrawImage(tmp, e.Bounds.Left, e.Bounds.Top, cbAction.ItemHeight, cbAction.ItemHeight);
                    }
                }
                catch
                {
                    //No icon to draw
                }
            }
            catch
            {
                e.Graphics.FillRectangle(currentSkin.ItemsBoxSelectedBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
            }
        }
        #endregion
        #region cbParameters
        void cbParameters_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            try
            {
                IItem temp = ((List<IItem>)(lbParams.DataSource))[e.Index];
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    e.Graphics.FillRectangle(currentSkin.ItemsBoxSelectedBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                    if (DOMercury.Engine.Interface.EngineCore.HasSubIndex(temp.ItemType))
                    {
                        //Bitmap tmp = IconManager.GetIconImage("Dive Arrow");
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxSelectedTextBrush, lbParams.ItemHeight + 10, e.Bounds.Top);
                        //e.Graphics.DrawImage(tmp, lbParams.ItemHeight, e.Bounds.Top, 10, 10);
                    }
                    else
                    {
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxSelectedTextBrush, lbParams.ItemHeight, e.Bounds.Top);
                    }
                    if (DR)
                    {
                        Font f = new Font(currentSkin.ItemsBoxFont, FontStyle.Italic);
                        e.Graphics.DrawString(temp.DisplayDesc, f, currentSkin.ItemsBoxSelectedTextBrush, lbParams.ItemHeight * 1.5F, e.Bounds.Top + (lbParams.ItemHeight / 2));
                    }
                }
                else
                {
                    e.Graphics.FillRectangle(currentSkin.ItemsBoxBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                    if (DOMercury.Engine.Interface.EngineCore.HasSubIndex(temp.ItemType))
                    {
                        //Bitmap tmp = IconManager.GetIconImage("Dive Arrow");
                        e.Graphics.FillRectangle(currentSkin.ItemsBoxBackBrush, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height));
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxTextBrush, lbParams.ItemHeight + 10, e.Bounds.Top);
                        //e.Graphics.DrawImage(tmp, lbParams.ItemHeight, e.Bounds.Top, 10, 10);
                    }
                    else
                    {
                        e.Graphics.DrawString(temp.DisplayName, currentSkin.ItemsBoxFont, currentSkin.ItemsBoxTextBrush, lbParams.ItemHeight, e.Bounds.Top);
                    }
                    if (DR)
                    {
                        Font f = new Font(currentSkin.ItemsBoxFont, FontStyle.Italic);
                        e.Graphics.DrawString(temp.DisplayDesc, f, currentSkin.ItemsBoxTextBrush, lbParams.ItemHeight * 1.5F, e.Bounds.Top + (lbParams.ItemHeight / 2));
                    }
                }

                try
                {
                    Bitmap tmp = DOMercury.Engine.Interface.EngineCore.GetIconImage(temp.DisplayDesc);
                    if (!(tmp == null))
                    {
                        e.Graphics.DrawImage(tmp, e.Bounds.Left, e.Bounds.Top, lbParams.ItemHeight, lbParams.ItemHeight);
                    }
                }
                catch
                {
                    //No icon to draw
                }
            }
            catch
            {
                //something terrible happened, do not draw
            }
            
        }

        private void cbParameters_Enter(object sender, EventArgs e)
        {
            gbParameters_Enter(sender, e);
            if (lbParams.DataSource != null)
            {
                
                lbParams.Size = new Size(lbParams.Size.Width, lbParams.ItemHeight * ((List<IItem>)(lbParams.DataSource)).Count);
                lbParams.Visible = true;
                lbParams.BringToFront();
            } 
        }

        private void cbParameters_Leave(object sender, EventArgs e)
        {
            lbParams.Visible = false;
        }

        void cbParameters_KeyDown(object sender, KeyEventArgs e)
        {
           switch (e.KeyCode)
            {
                case Keys.Up:
                    if (lbParams.DataSource != null)
                    {
                        if ((((List<IItem>)(lbParams.DataSource)).Count > 0) && (!e.Shift))
                        {
                            if (lbParams.SelectedIndex == -1)
                            {
                                lbParams.SelectedIndex = 0;
                            }
                            else if (lbParams.SelectedIndex > 0)
                            {
                                lbParams.SelectedIndex--;
                            }
                            if (!e.Shift)
                            {
                                e.SuppressKeyPress = true;
                            }
                        }
                    }
                    break;
                case Keys.Down:
                    if (lbParams.DataSource != null)
                    {
                        if ((((List<IItem>)(lbParams.DataSource)).Count > 0) && (!e.Shift))
                        {
                            if (lbParams.SelectedIndex == -1)
                            {
                                lbParams.SelectedIndex = 0;
                            }
                            else if (lbParams.SelectedIndex < ((List<IItem>)(lbParams.DataSource)).Count - 1)
                            {
                                lbParams.SelectedIndex++;
                            }
                            if (!e.Shift)
                            {
                                e.SuppressKeyPress = true;
                            }
                        }
                    }
                    break;
                default:
                    break;

            } 
        }

        private void cbParameters_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                ResetForm();
                this.Hide();
                
            }
            else if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
            {
                //SendKeys.Send("{TAB}");
                if (cbAction.DroppedDown)
                {
                    cbAction.DroppedDown = false;
                }

                //Handle Execute function
                EnterButtonHandler();
            }
            else
            {
                //if (cbParameters.AutoCompleteSource != AutoCompleteSource.FileSystemDirectories)
                //{
                
                //}
            }
        }

        #endregion
        #region DOMercury Form Movement
        private void pnlMove_MouseDown(object sender, MouseEventArgs e)
        {
            moving = true;
            clickpoint = new Point(e.X, e.Y);
            
        }

        private void pnlMove_MouseMove(object sender, MouseEventArgs e)
        {
            if (moving)
            {
                Point mtp = this.PointToScreen(new Point(e.X, e.Y));
                mtp.Offset(clickpoint.X * -1, (clickpoint.Y + SystemInformation.CaptionHeight + SystemInformation.BorderSize.Height) * -1);
                this.Location = mtp;
                //bgForm.Location = this.Location;
            }
        }

        private void pnlMove_MouseUp(object sender, MouseEventArgs e)
        {
            moving = false;
        }
        #endregion
        #region pnlIcon
        private void pnlIcon_Paint(object sender, PaintEventArgs e)
        {
            //pnlIcon.Visible = true;
            if (lbItems.Items.Count > 0)
            {


                if (currIcon != null)
                {
                    Graphics g = e.Graphics;
                    g.DrawImage(currIcon, pnlIcon.DisplayRectangle);
                }
                else
                {
                    //No image to paint
                    pnlIcon.Visible = false;
                    //e.Graphics.Clear(Color.Transparent);
                }
            }
            else
            {
                //No image to paint
                //e.Graphics.Clear(Color.Transparent);
                pnlIcon.Visible = false;
            }
        }
        #endregion
        #region pnlItemIcon
        private void pnlItemIcon_Paint(object sender, PaintEventArgs e)
        {
            //pnlItemIcon.Visible = true;
            if (lbItems.Items.Count > 0)
            {


                if (currIcon != null)
                {
                    Graphics g = e.Graphics;
                    if (pIcon == null)
                    {
                        g.DrawImage(currIcon, pnlItemIcon.DisplayRectangle);
                    }
                    else
                    {
                        g.DrawImage(pIcon, pnlItemIcon.DisplayRectangle);
                    }
                }
                else
                {
                    //No image to paint
                    pnlItemIcon.Visible = false;
                    //e.Graphics.Clear(Color.Transparent);
                }
            }
            else
            {
                //No image to paint
                //e.Graphics.Clear(Color.Transparent);
                pnlItemIcon.Visible = false;
            }
        }
        #endregion
        #region pnlActionIcon
        private void pnlActionIcon_Paint(object sender, PaintEventArgs e)
        {

            if (ActionIcon != null)
            {
                Graphics g = e.Graphics;
                g.DrawImage(ActionIcon, pnlActionIcon.DisplayRectangle);
            }
            else
            {
                //No image to paint
                pnlActionIcon.Visible = false;
                //e.Graphics.Clear(Color.Transparent);
            }
        }
        #endregion
        #region pnlpIcon
        private void pnlpIcon_Paint(object sender, PaintEventArgs e)
        {
            //pnlIcon.Visible = true;
            if (DOMercury.Engine.Interface.EngineCore.ParentItem != null)
            {


                if (pIcon != null)
                {
                    Graphics g = e.Graphics;
                    g.DrawImage(pIcon, pnlpIcon.DisplayRectangle);
                }
                else
                {
                    //No image to paint
                    pnlpIcon.Visible = false;
                    //e.Graphics.Clear(Color.Transparent);
                }
            }
            else
            {
                //No image to paint
                //e.Graphics.Clear(Color.Transparent);
                pnlpIcon.Visible = false;
            }
        }
        #endregion
        
        void SearchTimer_Tick(object sender, EventArgs e)
        {
            SearchTimer.Stop();
            itemtext = cbitem.Text.ToLower();

            if (DOMercury.Engine.Interface.EngineCore.IsPath(itemtext))
            {
                type = "folder";
                if (cbitem.AutoCompleteSource != DOMercury.Engine.Interface.EngineCore.ItemAutoCompleteSource(type))
                {
                    cbitem.AutoCompleteSource = DOMercury.Engine.Interface.EngineCore.ItemAutoCompleteSource(type);
                    cbitem.SelectionStart = cbitem.Text.Length;
                    cbitem.SelectionLength = 0;
                }
            }
            if (type == "folder")
            {
                if (!(itemtext.Contains(":")))
                {
                    type = "text";
                    lbItems.Visible = true;
                    UpdatelbItems();
                    discoverType();
                    return;
                }
            }

            lbItems.Visible = true;

            if (((type != "folder")) || (cbitem.Text.Length == 0))
            {
                UpdatelbItems();
                discoverType();
                if ((cbitem.Text.Length == 0) && (!DOMercury.Engine.Interface.EngineCore.CurrentIndexIsMainIndex))
                {
                    if (lbObjects.Items.Count > 0)
                    {
                        TurnOffcbItem();
                        lbItems.Visible = false;
                        discoverType();
                        if (cbitem.AutoCompleteSource != DOMercury.Engine.Interface.EngineCore.ItemAutoCompleteSource(type))
                        {
                            cbitem.AutoCompleteSource = DOMercury.Engine.Interface.EngineCore.ItemAutoCompleteSource(type);
                            cbitem.SelectionStart = cbitem.Text.Length;
                            cbitem.SelectionLength = 0;
                        }
                    }

                }
                else
                {
                    
                }
            }
            else
            {
                UpdatelbItems();
                discoverType();
                //if (cbitem.AutoCompleteSource != AutoCompleteSource.FileSystemDirectories && (itemtext.Length > 2))
                //{
                //    AutoCompleteManager.ChangeItemAutoCompleteSource(ref cbitem, type);
                //}
            }
           
            getActions();
            ChangeDescriptions(true);
            ChangeIcon();
        }


#endregion

        
    }

    public class ConfigManager
    {
        public static bool Fade = true; 
        public static bool ShowIcons = true; 
        public static string CurrentSkin = "Skins/Default/Default.skin"; 
        public static bool DescriptiveResults = true; 
        public static int STI = 100;
        public static double Opacity = 1;

        public static void LoadCurrentConfiguration()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("Interfaces\\SkinnableFormConfig.xml");
            
                ShowIcons = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("ShowIcons").InnerText);
                DescriptiveResults = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("DescriptiveResults").InnerText);
                CurrentSkin = doc.DocumentElement.SelectSingleNode("CurrentSkin").InnerText;
                STI = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("STI").InnerText);
                Fade = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Fade").InnerText);
                Opacity = Convert.ToDouble(doc.DocumentElement.SelectSingleNode("Opacity").InnerText);
            }
            catch
            {
                //System.Windows.Forms.MessageBox.Show("Config.xml is malformed, a required element is missing.  Please fix or reinstall.");
            }
        }

        public static void SaveCurrentConfiguration()
        {
            XmlTextWriter w = new XmlTextWriter("Interfaces\\SkinnableFormConfig.xml", Encoding.ASCII);
            w.WriteStartElement("configuration");

            w.WriteStartElement("ShowIcons");
            w.WriteRaw(ShowIcons.ToString());
            w.WriteEndElement();
            w.WriteStartElement("CurrentSkin");
            w.WriteRaw(CurrentSkin);
            w.WriteEndElement();
            w.WriteStartElement("DescriptiveResults");
            w.WriteRaw(DescriptiveResults.ToString());
            w.WriteEndElement();
            w.WriteStartElement("STI");
            w.WriteRaw(STI.ToString());
            w.WriteEndElement();
            w.WriteStartElement("Fade");
            w.WriteRaw(Fade.ToString());
            w.WriteEndElement();
            w.WriteStartElement("Opacity");
            w.WriteRaw(Opacity.ToString());
            w.WriteEndElement();
            w.WriteEndElement();

            try
            {
                w.Close();
            }
            catch { }
        }
    }

}